home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Interfaces / UniversalInterfaces 2.1B1 / CIncludes / DigitalSignature.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-18  |  16.3 KB  |  397 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        DigitalSignature.h
  3.  
  4.      Contains:    Digital Signature Interfaces.
  5.  
  6.      Version:    Technology:    AOCE toolbox 1.02
  7.                  Package:    Universal Interfaces 2.1ß1 in “MPW Prerelease” on ETO #17
  8.  
  9.      Copyright:    © 1984-1995 by Apple Computer, Inc.
  10.                  All rights reserved.
  11.  
  12.      Bugs?:        If you find a problem with this file, use the Apple Bug Reporter
  13.                  stack.  Include the file and version information (from above)
  14.                  in the problem description and send to:
  15.                      Internet:    apple.bugs@applelink.apple.com
  16.                      AppleLink:    APPLE.BUGS
  17.  
  18. */
  19.  
  20. #ifndef __DIGITALSIGNATURE__
  21. #define __DIGITALSIGNATURE__
  22.  
  23.  
  24. #ifndef __TYPES__
  25. #include <Types.h>
  26. #endif
  27. /*    #include <ConditionalMacros.h>                                */
  28.  
  29. #ifndef __MEMORY__
  30. #include <Memory.h>
  31. #endif
  32. /*    #include <MixedMode.h>                                        */
  33.  
  34. #ifndef __FILES__
  35. #include <Files.h>
  36. #endif
  37. /*    #include <OSUtils.h>                                        */
  38.  
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42.  
  43. #if PRAGMA_ALIGN_SUPPORTED
  44. #pragma options align=mac68k
  45. #endif
  46.  
  47. #if PRAGMA_IMPORT_SUPPORTED
  48. #pragma import on
  49. #endif
  50.  
  51.  
  52. enum {
  53.     kSIGNewContext                = 1900,
  54.     kSIGDisposeContext            = 1901,
  55.     kSIGSignPrepare                = 1902,
  56.     kSIGSign                    = 1903,
  57.     kSIGVerifyPrepare            = 1904,
  58.     kSIGVerify                    = 1905,
  59.     kSIGDigestPrepare            = 1906,
  60.     kSIGDigest                    = 1907,
  61.     kSIGProcessData                = 1908,
  62.     kSIGShowSigner                = 1909,
  63.     kSIGGetSignerInfo            = 1910,
  64.     kSIGGetCertInfo                = 1911,
  65.     kSIGGetCertNameAttributes    = 1912,
  66.     kSIGGetCertIssuerNameAttributes = 1913,
  67.     kSIGFileIsSigned            = 2500,
  68.     kSIGSignFile                = 2501,
  69.     kSIGVerifyFile                = 2502
  70. };
  71.  
  72. enum {
  73.     kSIGCountryCode,
  74.     kSIGOrganization,
  75.     kSIGStreetAddress,
  76.     kSIGState,
  77.     kSIGLocality,
  78.     kSIGCommonName,
  79.     kSIGTitle,
  80.     kSIGOrganizationUnit,
  81.     kSIGPostalCode
  82. };
  83.  
  84. typedef unsigned short SIGNameAttributeType;
  85.  
  86. /* 
  87. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  88. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  89. the certificate is currently valid. kSIGPending means the certificate is
  90. currently not valid - but will be.  kSIGExpired means the certificate has
  91. expired. A time is always associated with a SIGCertStatus.  In each case the
  92. time has a specific interpretation.  When the status is kSIGValid the time is
  93. when the certificate will expire. When the status is kSIGPending the time is
  94. when the certificate will become valid. When the status is kSIGExpired the time
  95. is when the certificate expired. In the SIGCertInfo structure, the startDate
  96. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  97. structure, this information is provided in the certSetStatusTime field. In the
  98. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  99. field which can contain any of the types below. NOTE: The only time you will get 
  100. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  101. you get a signature that was created after the certificates expiration date, something
  102. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  103. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  104. possibly allowed by other platforms.
  105. */
  106. /* Values for SIGCertStatus or SIGSignatureStatus */
  107.  
  108. enum {
  109.     kSIGValid,                                                    /* possible for either a SIGCertStatus or SIGSignatureStatus */
  110.     kSIGPending,                                                /* possible for either a SIGCertStatus or SIGSignatureStatus */
  111.     kSIGExpired,                                                /* possible for either a SIGCertStatus or SIGSignatureStatus
  112.     * possible only for a SIGSignatureStatus */
  113.     kSIGInvalid
  114. };
  115.  
  116. typedef unsigned short SIGCertStatus;
  117.  
  118. typedef unsigned short SIGSignatureStatus;
  119.  
  120. /* Gestalt selector code - returns toolbox version in low-order word */
  121.  
  122. enum {
  123.     gestaltDigitalSignatureVersion = 'dsig'
  124. };
  125.  
  126. /* Number of bytes needed for a digest record when using SIGDigest */
  127. enum {
  128.     kSIGDigestSize                = 16
  129. };
  130.  
  131. typedef Byte SIGDigestData[kSIGDigestSize], *SIGDigestDataPtr;
  132.  
  133. struct SIGCertInfo {
  134.     unsigned long                    startDate;                    /* cert start validity date */
  135.     unsigned long                    endDate;                    /* cert end validity date */
  136.     SIGCertStatus                    certStatus;                    /* see comment on SIGCertStatus for definition */
  137.     unsigned long                    certAttributeCount;            /* number of name attributes in this cert */
  138.     unsigned long                    issuerAttributeCount;        /* number of name attributes in this certs issuer */
  139.     Str255                            serialNumber;                /* cert serial number */
  140. };
  141.  
  142. typedef struct SIGCertInfo SIGCertInfo;
  143.  
  144. typedef SIGCertInfo *SIGCertInfoPtr;
  145.  
  146. struct SIGSignerInfo {
  147.     unsigned long                    signingTime;                /* time of signing */
  148.     unsigned long                    certCount;                    /* number of certificates in the cert set */
  149.     unsigned long                    certSetStatusTime;            /* Worst cert status time. See comment on SIGCertStatus for definition */
  150.     SIGSignatureStatus                signatureStatus;            /* The status of the signature. See comment on SIGCertStatus for definition*/
  151. };
  152.  
  153. typedef struct SIGSignerInfo SIGSignerInfo;
  154.  
  155. typedef SIGSignerInfo *SIGSignerInfoPtr;
  156.  
  157. struct SIGNameAttributesInfo {
  158.     Boolean                            onNewLevel;
  159.     Boolean                            filler1;
  160.     SIGNameAttributeType            attributeType;
  161.     ScriptCode                        attributeScript;
  162.     Str255                            attribute;
  163. };
  164.  
  165. typedef struct SIGNameAttributesInfo SIGNameAttributesInfo;
  166.  
  167. typedef SIGNameAttributesInfo *SIGNameAttributesInfoPtr;
  168.  
  169. typedef Ptr SIGContextPtr;
  170.  
  171. typedef Ptr SIGSignaturePtr;
  172.  
  173. /*
  174. Certificates are always in order. That is, the signers cert is always 0, the
  175. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  176. You can use this constant for readability in your code.
  177. */
  178.  
  179. enum {
  180.     kSIGSignerCertIndex            = 0
  181. };
  182.  
  183. /*
  184. Call back procedure supplied by developer, return false to cancel the current
  185. process.
  186. */
  187. typedef pascal Boolean (*SIGStatusProcPtr)(void);
  188.  
  189. #if GENERATINGCFM
  190. typedef UniversalProcPtr SIGStatusUPP;
  191. #else
  192. typedef SIGStatusProcPtr SIGStatusUPP;
  193. #endif
  194.  
  195. enum {
  196.     uppSIGStatusProcInfo = kPascalStackBased
  197.          | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
  198. };
  199.  
  200. #if GENERATINGCFM
  201. #define NewSIGStatusProc(userRoutine)        \
  202.         (SIGStatusUPP) NewRoutineDescriptor((ProcPtr)(userRoutine), uppSIGStatusProcInfo, GetCurrentArchitecture())
  203. #else
  204. #define NewSIGStatusProc(userRoutine)        \
  205.         ((SIGStatusUPP) (userRoutine))
  206. #endif
  207.  
  208. #if GENERATINGCFM
  209. #define CallSIGStatusProc(userRoutine)        \
  210.         CallUniversalProc((UniversalProcPtr)(userRoutine), uppSIGStatusProcInfo)
  211. #else
  212. #define CallSIGStatusProc(userRoutine)        \
  213.         (*(userRoutine))()
  214. #endif
  215.  
  216. /*
  217. Resource id's of standard signature icon suite, all sizes and colors are available.
  218. */
  219. enum {
  220.     kSIGSignatureIconResID        = -16800,
  221.     kSIGValidSignatureIconResID    = -16799,
  222.     kSIGInvalidSignatureIconResID = -16798
  223. };
  224.  
  225. /* ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— 
  226. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  227. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  228. a context for you.  To free the memory occupied by the context and invalidate
  229. its internal data, call SIGDisposeContext. An initialized context has no notion
  230. of the type of operation it will be performing however, once you call
  231. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  232. type is set and to switch  to another type of operation will require creating a
  233. new context. Be sure to pass the same context to corresponding toolbox calls
  234. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  235. signing and verify calls with the same context is not allowed.
  236. */
  237. extern pascal OSErr SIGNewContext(SIGContextPtr *context)
  238.  FOURWORDINLINE(0x203C, 2, 1900, 0xAA5D);
  239. extern pascal OSErr SIGDisposeContext(SIGContextPtr context)
  240.  FOURWORDINLINE(0x203C, 2, 1901, 0xAA5D);
  241. /* ——————————————————————————————— SIGNING CALLS ——————————————————————————————— 
  242. Once you have created a SIGContextPtr, you create a signature by calling
  243. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  244. toRcpt SIGSign. To create another signature on different data but for the same
  245. signer, don't dispose of the context and call SIGProcessData for the new data
  246. followed by a call SIGSign again. In this case the signer will not be prompted
  247. for their signer and password again as it was already provided.  Once you call
  248. SIGDisposeContext, all signer information will be cleared out of the context and
  249. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  250. if you want the toolbox to use the last signer by default or prompt for a signer
  251. if none exists.  The prompt parameter can be used to pass a string to be displayed
  252. in the dialog that prompts the user for their password.  If the substring "^1"
  253. (without the quotes) is in the prompt string, then the toolbox will replace it
  254. with the name of the signer from the signer selected by the user.  If an empty
  255. string is passed, the following default string will be sent to the toolbox
  256. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  257. or SIGSign to get information about the signer or certs.
  258. */
  259. extern pascal OSErr SIGSignPrepare(SIGContextPtr context, const FSSpec *signerFile, ConstStr255Param prompt, Size *signatureSize)
  260.  FOURWORDINLINE(0x203C, 8, 1902, 0xAA5D);
  261. extern pascal OSErr SIGSign(SIGContextPtr context, SIGSignaturePtr signature, SIGStatusUPP statusProc)
  262.  FOURWORDINLINE(0x203C, 6, 1903, 0xAA5D);
  263. /* ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— 
  264. Once you have created a SIGContextPtr, you verify a signature by calling
  265. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  266. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  267. verified or not (noErr is returned on  success otherwise the appropriate error
  268. code).  Upon successfull verification, you can call any of the utility routines
  269. toRcpt find out who signed the data.
  270. */
  271. extern pascal OSErr SIGVerifyPrepare(SIGContextPtr context, SIGSignaturePtr signature, Size signatureSize, SIGStatusUPP statusProc)
  272.  FOURWORDINLINE(0x203C, 8, 1904, 0xAA5D);
  273. extern pascal OSErr SIGVerify(SIGContextPtr context)
  274.  FOURWORDINLINE(0x203C, 2, 1905, 0xAA5D);
  275. /* —————————————————————————————— DIGESTING CALLS —————————————————————————————— 
  276. Once you have created a SIGContextPtr, you create a digest by calling
  277. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  278. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  279. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  280. digest.
  281. */
  282. extern pascal OSErr SIGDigestPrepare(SIGContextPtr context)
  283.  FOURWORDINLINE(0x203C, 2, 1906, 0xAA5D);
  284. extern pascal OSErr SIGDigest(SIGContextPtr context, SIGDigestData digest)
  285.  FOURWORDINLINE(0x203C, 4, 1907, 0xAA5D);
  286. /* —————————————————————————————— PROCESSING DATA —————————————————————————————— 
  287. To process data during a digest, sign, or verify operation call SIGProcessData
  288. as many times as necessary and with any sized blocks of data.  The data needs to
  289. be processed in the same order during corresponding sign and verify operations
  290. but does not need to be processed in the same sized chunks (i.e., the toolbox
  291. just sees it as a continuous bit stream).
  292. */
  293. extern pascal OSErr SIGProcessData(SIGContextPtr context, const void *data, Size dataSize)
  294.  FOURWORDINLINE(0x203C, 6, 1908, 0xAA5D);
  295. /* ——————————————————————————————— UTILITY CALLS ——————————————————————————————— 
  296. Given a context that has successfully performed a verification SIGShowSigner
  297. will  display a modal dialog with the entire distinguished name of the person
  298. who signed the data. the prompt (if supplied) will appear at the top of the
  299. dialog.  If no prompt is specified, the default prompt "\pVerification
  300. Successfull." will appear.
  301.  
  302. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  303. successful SIGVerify, you can make the remaining utility calls:
  304.  
  305. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  306. toRcpt index into the certificate set when calling SIGGetCertInfo,
  307. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  308. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  309. will tell you the best status of the entire certificate set while
  310. certSetStatusTime will correspond to the time associated with that status (see
  311. definitions above).
  312.  
  313. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  314. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  315. always the signers certificate.  The  serial number, start date and end date
  316. are there should you wish to display that info.  The  certAttributeCount and
  317. issuerAttributeCount provide the number of parts in the name of that certificate
  318. or that certificates issuer respectively.  You use these numbers to index into
  319. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  320. the name. The certStatus will tell you the status of the certificate while
  321. certStatusTime will correspond to the time associated with that status (see
  322. definitions above).
  323.  
  324. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  325. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  326. tells you wether the name attribute returned is at the same level in the name
  327. hierarchy as the previous attribute.  The type return value tells you  the type
  328. of attribute returned. nameAttribute is the actual string containing the name
  329. attribute.   So, if you wanted to display the entire distinguished name of the
  330. person who's signature was just validated you could do something like this;
  331.  
  332.     (…… variable declarations and verification code would preceed this sample ……)
  333.  
  334.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  335.     HandleErr(error);
  336.  
  337.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  338.         {
  339.         error = SIGGetCertNameAttributes(
  340.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  341.         HandleErr(error);
  342.         DisplayNamePart(theAttribute, type, newLevel);
  343.         }
  344. */
  345. extern pascal OSErr SIGShowSigner(SIGContextPtr context, ConstStr255Param prompt)
  346.  FOURWORDINLINE(0x203C, 4, 1909, 0xAA5D);
  347. extern pascal OSErr SIGGetSignerInfo(SIGContextPtr context, SIGSignerInfo *signerInfo)
  348.  FOURWORDINLINE(0x203C, 4, 1910, 0xAA5D);
  349. extern pascal OSErr SIGGetCertInfo(SIGContextPtr context, unsigned long certIndex, SIGCertInfo *certInfo)
  350.  FOURWORDINLINE(0x203C, 6, 1911, 0xAA5D);
  351. extern pascal OSErr SIGGetCertNameAttributes(SIGContextPtr context, unsigned long certIndex, unsigned long attributeIndex, SIGNameAttributesInfo *attributeInfo)
  352.  FOURWORDINLINE(0x203C, 8, 1912, 0xAA5D);
  353. extern pascal OSErr SIGGetCertIssuerNameAttributes(SIGContextPtr context, unsigned long certIndex, unsigned long attributeIndex, SIGNameAttributesInfo *attributeInfo)
  354.  FOURWORDINLINE(0x203C, 8, 1913, 0xAA5D);
  355. /* ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— 
  356. These calls allow you to detect the presence of a standard signtaure in a file as 
  357. well as sign and verify files in a standard way.  An example of this is the Finder, 
  358. which uses these calls to allow the user to "drop sign" a file.
  359.  
  360. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  361. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  362. be returned.
  363.  
  364. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  365. a standard way: 
  366.  
  367. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  368. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  369. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  370. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  371. it is replaced with the newly created signature.
  372.  
  373. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  374. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  375. to SIGShowSigner to display the name of the person who signed the file.
  376. */
  377. extern pascal OSErr SIGFileIsSigned(const FSSpec *fileSpec)
  378.  FOURWORDINLINE(0x203C, 2, 2500, 0xAA5D);
  379. extern pascal OSErr SIGSignFile(SIGContextPtr context, Size signatureSize, const FSSpec *fileSpec, SIGStatusUPP statusProc)
  380.  FOURWORDINLINE(0x203C, 8, 2501, 0xAA5D);
  381. extern pascal OSErr SIGVerifyFile(SIGContextPtr context, const FSSpec *fileSpec, SIGStatusUPP statusProc)
  382.  FOURWORDINLINE(0x203C, 6, 2502, 0xAA5D);
  383.  
  384. #if PRAGMA_IMPORT_SUPPORTED
  385. #pragma import off
  386. #endif
  387.  
  388. #if PRAGMA_ALIGN_SUPPORTED
  389. #pragma options align=reset
  390. #endif
  391.  
  392. #ifdef __cplusplus
  393. }
  394. #endif
  395.  
  396. #endif /* __DIGITALSIGNATURE__ */
  397.